#### iobr server 免疫浸润分析####

try({
  setwd("/rproject/r-language/lilongyuan2/go-kegg可视化/")
})
try({
  setwd('父级路径')
})


library(IOBR)
library(tidyverse)#数据处理
library(tidyHeatmap)
library(maftools)
library(ggpubr)#绘图
library(ggplot2)
library(survival)#生存分析
library(survminer)#生存曲线

#输入数据
mydata=read.table("GLIOMA_TPM_MRNA.txt",sep="\t",header=T,check.names=F)

# 找出重复的行名
any(duplicated(rownames(mydata)))
any(duplicated(colnames(mydata)))
dup_row_names <- rownames(mydata)[duplicated(rownames(mydata))]
if (length(dup_row_names) > 0) {
  print(paste("重复的行名:", paste(dup_row_names, collapse = ", ")))
} else {
  print("所有行名都是唯一的。")
}
mydata <- mydata[,-1]



#查看肿瘤微环境_反卷曲_算法
tme_deconvolution_methods
#返回特征估计的可用参数选项
signature_score_calculation_methods
#肿瘤微环境相关特征基因集，$显示具体基因
names(signature_tme)
#代谢相关基因集
names(signature_metabolism)
#与生物医学基础研究相关基因集
names(signature_tumor)
#所有免疫细胞的特征基因集
signature_collection
#查看参考基因集文献出处
signature_collection_citation


# mydata <- mydata[,1:50]
# write.table(mydata,file="mydata.txt",sep="\t",quote=F,col.names=T)

###方法一：CIBERSORT
#数据转换
try({
  cibersort <- deconvo_tme(eset = mydata,#基因表达矩阵，数值型矩阵/数据框，行名基因列名样本
                           method = "cibersort",#指定方法
                           arrays = F,#是否芯片数据
                           perm = 1#统计分析的置换次数（建议≥100）,cibersort用
  )
  write.table(cibersort,file = 'cibersort.txt')
  #绘图
  cell_bar_plot(input=cibersort[1:20,],#20个样本
                id = "ID",
                features = colnames(cibersort)[2:23],
                title="Cell Fraction cibersort",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  
  ggsave("Cibersort.pdf",width = 10,height = 8)
  ggsave("Cibersort.png",width = 10,height = 8)
})



try({
  ###方法二：EPIC
  #数据转换
  epic <- deconvo_tme(eset = mydata,#基因表达矩阵，数值型矩阵/数据框，行名基因列名样本
                      method = "epic",#指定方法
                      arrays = F,#是否芯片数据
  )
  write.table(epic,file = 'epic.txt')
  #绘图
  cell_bar_plot(input=epic[1:20,],#20个样本
                id = "ID",
                features = colnames(epic),
                title="Cell Fraction epic",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("epic.pdf",width = 10,height = 8)
  ggsave("epic.png",width = 10,height = 8)
})


###方法三：MCP
#数据转换
try({
  mcp <- deconvo_tme(eset = mydata,
                     method = "mcpcounter")
  
  write.table(mcp,file = 'mcp.txt')
  cell_bar_plot(input=mcp[1:20,],#20个样本
                id = "ID",
                features = colnames(mcp),
                title="Cell Fraction mcpcounter",
                legend.position="bottom",#图例位置
                palette=1,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  ggsave("mcpcounter.pdf",width = 10,height = 8)
  ggsave("mcpcounter.png",width = 10,height = 8)
})

###方法四：xCellibrary(devtools)
# library(devtools)
# devtools::install_local("GSVA")
#数据转换
try({
  xcell <- deconvo_tme(eset = mydata,
                       method = "xcell",
                       arrays = FALSE)
  #绘图
  write.table(xcell,file = 'xcell.txt')
  cell_bar_plot(input=xcell[1:20,],#20个样本
                id = "ID",
                features = colnames(xcell),
                title="Cell Fraction xcell",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("xcell.pdf",width = 10,height = 8)
  ggsave("xcell.png",width = 10,height = 8)
})


###方法五：ESTIMATE
#数据转换

try({
  estimate <- deconvo_tme(eset = mydata,
                          method = "estimate")
  
  write.table(estimate,file = 'estimate.txt')
  
  #绘图
  cell_bar_plot(input=estimate[1:20,],#20个样本
                id = "ID",
                features = colnames(estimate),
                title="Cell Fraction estimate",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("estimate.pdf",width = 10,height = 8)
  ggsave("estimate.png",width = 10,height = 8)
  
})


###方法六：TIMER
#数据转换
try({
  timer <- deconvo_tme(eset = mydata,
                       method = "timer",
                       group_list = rep("stad",dim(mydata)[2]))
  
  write.table(timer,file = 'timer.txt')
  
  #绘图
  cell_bar_plot(input=timer[1:20,],#20个样本
                id = "ID",
                features = colnames(timer),
                title="Cell Fraction timer",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("timer.pdf",width = 10,height = 8)
  ggsave("timer.png",width = 10,height = 8)
  
})


###方法七：quantiseq
#数据转换
try({
  quantiseq <- deconvo_tme(eset = mydata,
                           tumor = TRUE,
                           arrays = FALSE,
                           scale_mrna = TRUE,
                           method = "quantiseq")
  write.table(quantiseq,file = 'quantiseq.txt')
  
  #绘图
  cell_bar_plot(input=quantiseq[1:20,],#20个样本
                id = "ID",
                features = colnames(quantiseq),
                title="Cell Fraction quantiseq",
                legend.position="bottom",#图例位置
                palette=3,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("quantiseq.pdf",width = 10,height = 8)
  ggsave("quantiseq.png",width = 10,height = 8)
})

###方法八：IPS
#数据转换

try({
  ips <- deconvo_tme(eset = eset_stad,
                     method = "ips",
                     plot= FALSE)
  write.table(ips,file = 'ips.txt')
  
  #绘图
  cell_bar_plot(input=ips[1:20,],#20个样本
                id = "ID",
                features = colnames(ips),
                title="Cell Fraction ips",
                legend.position="bottom",#图例位置
                palette=2,#四种画板
                coord_filp = T,#是否翻转坐标轴
                show_col = F#是否显示细胞类型的颜色
  )
  
  ggsave("ips.pdf",width = 10,height = 8)
  ggsave("ips.png",width = 10,height = 8)
  
  
})

